/** * Copyright (c) 2016-2019 人人开源 All rights reserved. */

package com.joinval.modules.app.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.joinval.common.exception.JoinvalException;
import com.joinval.common.utils.R;
import com.joinval.common.validator.Assert;
import com.joinval.modules.app.dao.UserDao;
import com.joinval.modules.app.entity.UserEntity;
import com.joinval.modules.app.form.LoginForm;
import com.joinval.modules.app.service.UserService;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.stereotype.Service;


@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {

	@Override
	public UserEntity queryByMobile(String mobile) {
		return baseMapper.selectOne(new QueryWrapper<UserEntity>().eq("mobile", mobile));
	}

	@Override
	public long login(LoginForm form) {
		UserEntity user = queryByMobile(form.getMobile());
		Assert.isNull(user, "用户信息缺失");

		// 判断账号锁定
		if (user.getStatus() == 0) {
			throw new JoinvalException("账号已被锁定,请联系管理员");
		}
		// 验证密码
		if(!user.getPassword().equals(new Sha256Hash(form.getPassword(), user.getSalt()).toHex())){
			throw new JoinvalException("手机号或密码错误");
		}

		return user.getUserId();
	}
}
